package com.feiyumall.account.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.feiyu.common.utils.R;
import com.feiyumall.account.entity.redis.accountInfoEntity;
import com.feiyumall.account.service.tokenService;
import com.feiyumall.account.util.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @Author: Doate
 * @Date: Created in 2021/4/10 14:24
 * @Description:
 */

@Service
public class tokenServiceImpl implements tokenService {

    @Resource
    RedisUtil redisUtil;

    @Override
    public String addToken(accountInfoEntity info) {
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisUtil.set(token, info, 600);
        return token;
    }

    @Override
    public boolean checkToken(String token) {

        if (token != null) {
            if (redisUtil.hasKey(token)) {
                redisUtil.expire(token, 600);
                return true;
            }
        }

        return false;
    }

    @Override
    public accountInfoEntity getAccount(String token) {

        if (redisUtil.hasKey(token)) {
            return (accountInfoEntity) redisUtil.get(token);
        }

        return null;
    }

    @Override
    public void quit(String token) {

        if (token != null) {
            if (redisUtil.hasKey(token)) {
                redisUtil.del(token);
            }
        }

    }
}
